package com.jww.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jww.domain.User;
import com.jww.service.UserServiceImpl;
import com.jww.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    private UserService service = new UserServiceImpl();
    private ObjectMapper mapper = new ObjectMapper();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");

        if (method.equals("findAll")) {
            findAll(req, resp);
        }
        if (method.equals("insert")) {
            insert(req, resp);
        }
        if (method.equals("update")) {
            update(req, resp);
        }
        if (method.equals("deleteById")) {
            deleteById(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    // 查询所有
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {

        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");

        List<User> users = service.findAll();

        try {
            //将对象解析成字符串
            String json = mapper.writeValueAsString(users);
            //响应json数据
            resp.getWriter().write(json);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    //新增功能
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");
        // 1 获取页面数据
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        service.insert(username, email);

    }

    //删除
    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");

        //获取ids
        String id = req.getParameter("id");
        service.deleteById(id);

    }

    //修改
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //设置字符集
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");

        //获取
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String email = req.getParameter("email");


        service.update(id,username,email);

    }


}
